#include "../source/server.hpp"

class EchoServer
{
private:
    TcpServer _server;
private:
    void OnEstablished(const ConnPtr &conn) { DLOG("create connection: %p", conn.get()); }

    void OnClose(const ConnPtr &conn) { DLOG("close connection: %p", conn.get()); }

    void OnMessage(const ConnPtr &conn, Buffer *buf)
    {
        // echoServer的业务逻辑处理非常简单，只需要将对方发送的消息再转回发送给对方即可
        conn->Send(buf->ReadAddress(), buf->ReadableSize());
        buf->MoveReadIdx(buf->ReadableSize());
    }

public:
    EchoServer(uint16_t port)
        : _server(port)
    {
        _server.SetThreadCount(1);
        // _server.OpenInactiveRelease(5);
        _server.SetEstablishCbUsr(std::bind(&EchoServer::OnEstablished, this, std::placeholders::_1));
        _server.SetCloseCbUsr(std::bind(&EchoServer::OnClose, this, std::placeholders::_1));
        _server.SetMessageCbUsr(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }

    void Run() { _server.Run(); }

};
